﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AVGMachine : MonoBehaviour {

	public enum STATE
	{
		OFF,
		TYPING,
		PAUSED,
		CHOICE
	}

	public STATE state;

	private bool justEnter;

	//public List<string> contents;
	public Story01 data;
	public AVGAssetConfig asset;
	public UIPanel panel;

	[Range(1,100)]
	public float typingSpeed = 20f;


	[SerializeField]
	private int curLine;

	private string targetString;
	//[SerializeField]
	private float timerValue; // 不需要在unity中查看

	// Use this for initialization
	void Start () {
		//Init();
		state = STATE.OFF;
		justEnter = true;
	}
	
	// Update is called once per frame
	void Update () {
		switch (state)
		{
			case STATE.OFF:
				if( justEnter )
				{
					// Just Enter OFF state
					// Init UIPanel here
					Init();
					justEnter = false;
					LoadCharaTexture(asset.charaATex, asset.charaBTex);
				}
				break;

			case STATE.TYPING:
				if (justEnter)
				{
					ShowUI(); // 第一次进来，显示UI（对话框）
					// Just Enter TYPING state
					justEnter = false;
					LoadContent(data.dataList[curLine].Dialogtext, data.dataList[curLine].Charaadisplay, data.dataList[curLine].Charabdisplay);
					timerValue = 0;
				}
				CheckTypingFinished();
				UpdateContentString();
				break;

			case STATE.PAUSED:
				if (justEnter)
				{
					// Just Enter PAUSED state
					justEnter = false;
				}
				break;

			case STATE.CHOICE:
				if (justEnter)
				{
					// Just Enter PAUSED state
					// Init 2 Buttons here
					justEnter = false;
					panel.ShowButtons(true);
					panel.SetButtonNames(data.dataList[curLine].Btnamsg, data.dataList[curLine].Btnbmsg);
					panel.SetButtonTexts(data.dataList[curLine].Btnatext, data.dataList[curLine].Btnbtext);
				}
				break;

			default:
				break;
		}
		//// open dialog
		//if(Input.GetKeyDown("6"))
		//{
		//	Init();
		////	LoadText(data.contents[curLine].dialogText);
		//	LoadCharaTexture(asset.charaATex, asset.charaBTex);
			
		//	ShowUI();
		//}

		//if(Input.GetMouseButtonDown(0))
		//{
		//	NextLine();
		//	if(curLine >= data.dataList.Count)
		//	{
		//		curLine = data.dataList.Count;
		//		Init(); // Close UI Panel automatically when dialog finished.
		//	}
		////	LoadText(data.contents[curLine].dialogText);
		//	LoadContent(data.dataList[curLine].Dialogtext, data.dataList[curLine].Charaadisplay, data.dataList[curLine].Charabdisplay);
		//}

	}

	public void StartAVG()
	{
		if(state == STATE.OFF)
		{
			Cursor.visible = true;
			Cursor.lockState = CursorLockMode.None;
			GoToState(STATE.TYPING);
		}
		
	}

	public void UserClicked()
	{
		switch (state)
		{
			//case STATE.OFF:
			//	GoToState(STATE.TYPING);
			//	break;

			case STATE.TYPING:
				break;

			case STATE.PAUSED:
				NextLine();
				if(curLine >= data.dataList.Count)
				{
					GoToState(STATE.OFF);

					// Dialog Finished
					Cursor.visible = false;
					Cursor.lockState = CursorLockMode.Locked;
				}
				else
				{
					GoToState(STATE.TYPING);
				}
				break;
			default:
				break;
		}
	}

	private void CheckTypingFinished()
	{
		if(state == STATE.TYPING)
		{
			//GoToState(STATE.PAUSED);
			if (Mathf.FloorToInt(timerValue * typingSpeed) >= targetString.Length)
			{
				if (data.dataList[curLine].Ischoice)
				{
					GoToState(STATE.CHOICE);
					//return;
					// return;可有可无
				}
				else
				{
					GoToState(STATE.PAUSED);
				}
			}
		}
	}

	private void GoToState(STATE next)
	{
		state = next;
		justEnter = true;
	}

	private void Init()
	{
		HideUI();
		curLine = 0; // OFF状态会调用Init()函数，重置curLine
		panel.SetContentText("");
		LoadContent(data.dataList[curLine].Dialogtext, data.dataList[curLine].Charaadisplay, data.dataList[curLine].Charabdisplay);
		panel.ShowButtons(false);
	}

	void ShowUI()
	{
		panel.ShowCanvas(true);
	}

	void HideUI()
	{
		panel.ShowCanvas(false);
	}

	void NextLine()
	{
		curLine++;
	}

	//void LoadText(string value)
	//{
	//	panel.SetContentText(value);
	//}

	void LoadContent(string value, bool charADisplay, bool charBDisplay)
	{
		//panel.SetContentText(value);
		// 制作打字机效果
		targetString = value;
		panel.ShowCharaA(charADisplay);
		panel.ShowCharaB(charBDisplay);
	}

	// 更换为新的图片
	void LoadCharaTexture(Texture charaATex, Texture charaBTex)
	{
		panel.ChangeCharaATex(charaATex);
		panel.ChangeCharaBTex(charaBTex);
	}

	void UpdateContentString()
	{
		timerValue += Time.deltaTime;
		string tempString = targetString.Substring(0, Mathf.Min( Mathf.FloorToInt(timerValue * typingSpeed), targetString.Length));
		panel.SetContentText(tempString);
	}

	public void ProcessMSG(GameObject obj)
	{
		switch(obj.name)
		{
			case "GoHome":
				print("Go home is pressed.");
				Story01 tempStory = Resources.Load<Story01>("Story02"); // Story02.asset改为Story02
				data = tempStory;

				Init();
				justEnter = false;
				LoadCharaTexture(asset.charaATex, asset.charaBTex);
				GoToState(STATE.TYPING);
				break;

			case "GoShopping":
				print("Go shopping is pressed.");
				GameObject.FindGameObjectWithTag("door").SendMessage("OpenDoor");
				GoToState(STATE.OFF);
				break;
			default:
				break;
		}
	}
}
